import {
    homeUI
} from "../../ui/layaMaxUI";
import Util from "../util/Util";

export default class home extends homeUI {

    constructor() {
        super();
        //弄个全局遮罩
    }

    onEnable() {
        var parent = this;
        window.currPage = this;
        // window.currPageUtil = Util;

        var rightW = Laya.stage.width - 800;//右侧宽度
        this["右侧"].width = rightW;
        this["右侧背景"].width = rightW;
        this["模式框"].x = parseInt((rightW - 278) / 2);

        this.addMoshi();
        setTimeout(function () {
            parent.initEvent();
        }, 100);
    }

    initEvent() {
        //初始化点击按钮
        var parent = this;
        parent["每日好礼"].setTapCall(function () {
            console.log("每日好礼");
            parent.openMeiriLibao();
        });
        parent["荣誉按钮"].setTapCall(function () {
            console.log("荣誉按钮");
            parent.openChengjiu();
        });
        parent["设置按钮"].setTapCall(function () {
            console.log("设置按钮");
            parent.openSheZhi();
        });
        parent["钻石增加"].setTapCall(function () {
            console.log("钻石增加");
            parent.openZuanshi();
        });
    }


    addMoshi() {
        var parent = this;
        //添加模式
        for (var i = 1; i <= 5; i++) {
            this.addOneMoshi(i);
        }

        // setTimeout(function () {
        //     parent.gameFun();
        // }, 100);
    }
    addOneMoshi(i) {
        var parent = this;

        var one = Laya.ClassUtils.getInstance("ui.onegamemoshiUI");
        var cfgpath = Util.cfg.moshi[i];
        one.pos(cfgpath.x, 100 + cfgpath.y);
        one["图片" + i].visible = true;
        one["文字" + i].visible = true;
        this["模式框"].addChild(one);

        var islock = false;
        if (i == 5) {
            //上锁
            islock = true;
        }

        if (islock) {
            //上锁
            one["图片" + i].alpha = 0.2;
            one["文字" + i].alpha = 0.5;
            one["正常背景"].visible = false;
            one["上锁背景"].visible = true;
            one["上锁图"].visible = true;
        }

        setTimeout(function () {
            one["点击"].setTapCall(function () {
                console.log("点击", i, cfgpath.name);
                parent.gameFun();
            });
            if (islock) {
                one["点击"].setNoScale();
            }
        }, 100);
    }

    openZuanshi() {
        var parent = this;
        this["钻石弹出框"].visible = true;
        this["钻石弹出框box"].x = (Laya.stage.width - 639) / 2;
        this["钻石弹出框box获得"].text = "获得：100";
        this["钻石弹出框box关闭"].setTapCall(function () {
            console.log("钻石弹出框box关闭");
            parent["钻石弹出框"].visible = false;
        });
        this["钻石弹出框box领取"].setTapCall(function () {
            console.log("钻石弹出框box领取");
            parent["钻石弹出框"].visible = false;
        });
    }

    openSheZhi() {
        //设置
        var parent = this;
        var one = Laya.ClassUtils.getInstance("ui.shezhiUI");
        this.addChild(one);
        one.size(Laya.stage.width, Laya.stage.height);
        one["设置层"].x = (Laya.stage.width - 280) / 2;

        one["音乐"].texture = (Util.cachedata.muc != 1) ? "res/shezhi/muc1_2.png" : "res/shezhi/muc1_1.png";
        one["音效"].texture = (Util.cachedata.yinxiao != 1) ? "res/shezhi/yin1_2.png" : "res/shezhi/yin1_1.png";

        one["音效"].setTapCall(function () {
            console.log("音效");
            if (Util.cachedata.yinxiao == 1) {
                Util.cachedata.yinxiao = 0;
            } else {
                Util.cachedata.yinxiao = 1;
            }
            Util.saveData();
            one["音效"].texture = (Util.cachedata.yinxiao != 1) ? "res/shezhi/yin1_2.png" : "res/shezhi/yin1_1.png";
        });
        one["音乐"].setTapCall(function () {
            console.log("音乐");
            if (Util.cachedata.muc == 1) {
                Util.cachedata.muc = 0;
            } else {
                Util.cachedata.muc = 1;
            }
            Util.saveData();
            one["音乐"].texture = (Util.cachedata.muc != 1) ? "res/shezhi/muc1_2.png" : "res/shezhi/muc1_1.png";
        });

        one["返回"].setTapCall(function () {
            console.log("返回");
            one.destroy();
        });
    }


    openMeiriLibao() {
        //每日礼包
        var parent = this;
        var one = Laya.ClassUtils.getInstance("ui.meirihaoliUI");
        this.addChild(one);
        one.size(Laya.stage.width, Laya.stage.height);
        var rightW = Laya.stage.width - 840;//右侧宽度
        one["右侧"].width = rightW;
        one["右侧背景"].width = rightW;
        one["领取层"].x = (rightW - 280) / 2;

        one["返回"].setTapCall(function () {
            console.log("返回");
            one.destroy();
        });


        var col = 0, row = 0;
        for (var i = 0; i < 30; i++) {
            var oneday = Laya.ClassUtils.getInstance("ui.onedayUI");
            var x = col * 155;
            var y = row * 207;
            col++;
            if (col == 5) {
                oneday["右侧"].visible = false;
            }
            oneday["天"].text = "第" + (i + 1) + "天";
            oneday["已领取icon"].visible = i < 6;
            oneday["当前底框"].visible = i == 6;
            oneday["高端"].visible = i >= 4 && (i % 4 == 0);
            if (col >= 5) {
                col = 0;
                row++;
            }
            oneday.pos(x, y);
            one["左侧列表box"].addChild(oneday);
        }
    }


    openChengjiu() {
        //成就
        var parent = this;
        var one = Laya.ClassUtils.getInstance("ui.chengjiuUI");
        this.addChild(one);
        one.size(Laya.stage.width, Laya.stage.height);
        one["成就title"].x = (Laya.stage.width - 255) / 2;
        one["成就列表"].x = (Laya.stage.width - 450) / 2;

        for (var i = 0; i < 20; i++) {
            var oneitem = Laya.ClassUtils.getInstance("ui.onechengjiuUI");
            oneitem.y = i * 110;
            oneitem["icon"].texture = "res/chengjiu/item/" + i + ".png";
            if (Math.random() > 0.5) {
                oneitem["锁"].visible = false;
                oneitem["数量"].text = 100 + i;
            }
            one["成就列表box"].addChild(oneitem);
        }

        one["返回"].setTapCall(function () {
            console.log("返回");
            one.destroy();
        });
    }

    gameFun() {
        // Laya.timer.clear(this, this.runFun);
        this.destroy(true);
        Laya.stage.addChild(new Util.gameUi());
    }

    runFun() {

    }

    onDestroy() {

    }
}